package top.kaoshanji.p1ch02c;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/1/2 20:20
 */
public class MethodHandleExample07 {

    private static final Logger logger = LogManager.getLogger(MethodHandleExample07.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02c-13：MethodHandle的基本类型参数的绑定方式示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mh = lookup.findVirtual(String.class, "substring",
                MethodType.methodType(String.class, int.class, int.class));

        mh = mh.asType(mh.type().wrap());
        mh = mh.bindTo("Hello World").bindTo(1);
        System.out.println(mh.invoke(5));

        logger.info("...值是Hello World，调用String#substring方法，参数是3、5：{}...", "Hello World".substring(3, 5));

    }

}
